$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        init:function () {
            this.funObj().loadWidth();
            $('#list_province,#list_city,#list_county,#stats_type').select2();
            this.funObj().doSearch();
            this.listener();
        },
        listener:function () {
            var $this = this;

            //省改变
            $('#list_province').change(function () {
                $this.funObj().loadArea($(this).val(),'list_city', '全部市');
                $this.funObj().loadArea(0,'list_county', '全部区');
                $('#currentPage').val(1);
                $this.funObj().doSearch();
            });
            //市改变
            $('#list_city').change(function () {
                $this.funObj().loadArea($(this).val(),'list_county', '全部区');
                $('#currentPage').val(1);
                $this.funObj().doSearch();
            });
            //区改变
            $('#list_county').change(function () {
                $('#currentPage').val(1);
                $this.funObj().doSearch();
            });

            //统计方式
            $('#stats_type').change(function () {
                $this.funObj().triggerLoad();
            });
            //搜索
            $('#btn_search').click(function () {
                $this.funObj().doSearch();
            });
            //分页数目
            $('#pageSize').change(function () {
                $this.funObj().doSearch();
            });
            //上一页
            $('#btn_prefPage').click(function () {
                $this.funObj().prefPage();
            });
            //下一页
            $('#btn_nextPage').click(function () {
                $this.funObj().nextPage();
            });
            //学校名称
            $('#schoolName').change(function () {
                $('#currentPage').val(1);
            });

            //回车监听事件
            xhTool.bindDocKeyDown($('#btn_search'));

        },
        funObj:function () {
            var $this = this;
            return {
                loadArea:function (areaCode,seletor, placeHolder) {
                    xhExtra.ajaxRequest('ims/stats/getAreaByCode', {code:areaCode},{}, function (res) {
                        var str = '<option value="0" selected>'+placeHolder+'</option>';
                        if (res.length > 0) {
                            $.each(res,function (i,item) {
                                str +='<option value="'+item['id']+'">'+item['text']+'</option>';
                            });
                        }
                        $('#'+seletor).empty().append(str);
                    })
                },
                loadWidth:function() { //根据页面大小 变化 图标大小
                    var width  = ($(document.body).outerWidth(true) - 40) /3.0;
                    var height = $(document.body).outerHeight(true) - 170;
                    $('#gridView').attr('style','width: 100%;height: '+height+'px;');
                    $('#gridView .row div').each(function () {
                        $(this).attr('style','height:'+width+'px;');
                    });
                    window.onresize = function(){  //重新定义界面大小处理
                        var width  = ($(document.body).outerWidth(true) - 40) /3.0;
                        var height = $(document.body).outerHeight(true) - 170;
                        $('#gridView').attr('style','width: 100%;height: '+height+'px;');
                        $('#gridView .row div').each(function () {
                            $(this).attr('style','height:'+width+'px;');
                            $(this).find('iframe').attr('style', 'height:'+width+'px;width:100%;');
                            $(this).find('iframe')[0].contentWindow.obj.funObj().loadPayLine();
                        });
                    };
                },
                triggerLoad:function () {
                    $('#gridView .row div iframe').each(function () {
                        $(this)[0].contentWindow.obj.funObj().loadPayLine();
                    });
                },
                prefPage:function () {
                    var $page = parseInt($('#currentPage').val());
                    if ($page -1 <= 0) {
                        window.top.xhTop.showError('错误','已至第一页');
                        return false;
                    }
                    $('#currentPage').val($page - 1);
                    $this.funObj().doSearch();
                },
                nextPage:function () {
                    var $page = parseInt($('#currentPage').val());
                    var $totalPage = $('#totalPage').text();
                    if ($page + 1 > $totalPage) {
                        window.top.xhTop.showError('错误','已至最末页');
                        return false;
                    }
                    $('#currentPage').val($page + 1);
                    $this.funObj().doSearch();
                },
                // checkPageSize:function () {
                //     var $page = $('#currentPage').val();
                //     var $totalPage = $('#totalPage').val();
                //     // if (!$totalPage) {
                //     //     $totalPage = 1;
                //     // }
                //     if ($page < 0 || $page > $totalPage) {
                //         window.top.xhTop.showError('错误', '页数超过最大范围或小于零');
                //         return false;
                //     }
                //     return true;
                // },
                doSearch:function () {
                    var $page = $('#currentPage').val();
                    var $totalPage = parseInt($('#totalPage').text());
                    if ($page < 0 || $page > $totalPage) {
                        window.top.xhTop.showError('错误', '页数超过最大范围或小于零');
                        return false;
                    }
                    var params = {
                        page : $('#currentPage').val(),
                        size : $('#pageSize').val(),
                        province: $('#list_province').find("option:selected").text(),
                        city: $('#list_city').find("option:selected").text(),
                        county: $('#list_county').find("option:selected").text(),
                        key:$('#schoolName').val()
                    };

                    xhExtra.ajaxRequest('ims/stats/getSchoolPage',params, {}, function (res) {
                        $('#totalPage').text(res.totalPage);
                        $('#currentPage').val(res.currentPage);

                        if (res.currentPage > res.totalPage) {
                            $('#currentPage').val(1);
                            $this.funObj().doSearch();
                        }

                        $('#total').text(res.total);
                        var height  = ($(document.body).outerWidth(true) - 40) /3.0;
                        var str =  '<div class="row">\n';
                        $.each(res.rows, function (i,item) {
                            str +=  '                            <div class="col-md-12" style="height: '+height+'px;">\n' +
                                    '                                <iframe name="schoolFrame_'+i+'" src="'+baseUrl+'ims/stats/polyLine?schoolId='+item['iSchoolId']+'" frameborder="0"' +
                                    '                                        style="height: '+height+'px;width: 100%;"></iframe>\n' +
                                    '                            </div>\n' ;
                        });
                        str += '                        </div>';
                        $('#gridView').empty().append(str);
                    });
                },

            }
        }
    }
};